package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.engine.GlideException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import o.hg.e;
import o.sg.d;
import o.vk.c;

class vp<Model, Data> implements vk<Model, Data> {
    private final List<vk<Model, Data>> ˎ;
    private final e<List<Throwable>> ˏ;

    static class b<Data> implements sg<Data>, d<Data> {
        @Nullable
        private List<Throwable> ʻ;
        private final List<sg<Data>> ˊ;
        private final e<List<Throwable>> ˋ;
        private d<? super Data> ˎ;
        private rl ˏ;
        private int ॱ = 0;
        private boolean ॱॱ;

        b(@NonNull List<sg<Data>> list, @NonNull e<List<Throwable>> eVar) {
            this.ˋ = eVar;
            aas.ˏ((Collection) list);
            this.ˊ = list;
        }

        public void ˏ(@NonNull rl rlVar, @NonNull d<? super Data> dVar) {
            this.ˏ = rlVar;
            this.ˎ = dVar;
            this.ʻ = (List) this.ˋ.ˎ();
            ((sg) this.ˊ.get(this.ॱ)).ˏ(rlVar, this);
            if (this.ॱॱ) {
                ॱ();
            }
        }

        public void ˏ() {
            if (this.ʻ != null) {
                this.ˋ.ˋ(this.ʻ);
            }
            this.ʻ = null;
            for (sg ˏ : this.ˊ) {
                ˏ.ˏ();
            }
        }

        public void ॱ() {
            this.ॱॱ = true;
            for (sg ॱ : this.ˊ) {
                ॱ.ॱ();
            }
        }

        @NonNull
        public Class<Data> ˋ() {
            return ((sg) this.ˊ.get(0)).ˋ();
        }

        @NonNull
        public rz ˎ() {
            return ((sg) this.ˊ.get(0)).ˎ();
        }

        public void ॱ(@Nullable Data data) {
            if (data != null) {
                this.ˎ.ॱ(data);
            } else {
                ˊ();
            }
        }

        public void ˏ(@NonNull Exception exception) {
            ((List) aas.ˎ(this.ʻ)).add(exception);
            ˊ();
        }

        private void ˊ() {
            if (!this.ॱॱ) {
                if (this.ॱ < this.ˊ.size() - 1) {
                    this.ॱ++;
                    ˏ(this.ˏ, this.ˎ);
                    return;
                }
                aas.ˎ(this.ʻ);
                this.ˎ.ˏ(new GlideException("Fetch failed", new ArrayList(this.ʻ)));
            }
        }
    }

    vp(@NonNull List<vk<Model, Data>> list, @NonNull e<List<Throwable>> eVar) {
        this.ˎ = list;
        this.ˏ = eVar;
    }

    public c<Data> ˎ(@NonNull Model model, int i, int i2, @NonNull sc scVar) {
        int size = this.ˎ.size();
        List arrayList = new ArrayList(size);
        int i3 = 0;
        sa saVar = null;
        while (i3 < size) {
            sa saVar2;
            vk vkVar = (vk) this.ˎ.get(i3);
            if (vkVar.ˋ(model)) {
                c ˎ = vkVar.ˎ(model, i, i2, scVar);
                if (ˎ != null) {
                    saVar2 = ˎ.ॱ;
                    arrayList.add(ˎ.ˎ);
                    i3++;
                    saVar = saVar2;
                }
            }
            saVar2 = saVar;
            i3++;
            saVar = saVar2;
        }
        if (arrayList.isEmpty() || saVar == null) {
            return null;
        }
        return new c(saVar, new b(arrayList, this.ˏ));
    }

    public boolean ˋ(@NonNull Model model) {
        for (vk ˋ : this.ˎ) {
            if (ˋ.ˋ(model)) {
                return true;
            }
        }
        return false;
    }

    public String toString() {
        return "MultiModelLoader{modelLoaders=" + Arrays.toString(this.ˎ.toArray()) + '}';
    }
}
